home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 789 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  77 lines

  1. Path: gate.net!pslfl2-47
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP! File Pointers
  5. Date: 9 Jan 1996 06:08:51 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4ct0pj$19p2@news.gate.net>
  8. References: <4csr4c$fem@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: pslfl2-29.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4csr4c$fem@newsbf02.news.aol.com>,
  13.    roberino@aol.com (Roberino) wrote:
  14. >I am currently trying to keep one file open while opening other
  15. >files one at a time using a separate file pointer.  However, as
  16. >soon as I read a line from the second file, the first file pointer
  17. >somehow gets destroyed and set to some position in the newly
  18. >opened file.  Has anyone else encountered this?  And if so,
  19. >is there a solution? (i.e. A way to protect the first file pointer
  20. >from being overwritten.)
  21. >
  22. >Here are the steps I am performing:
  23. >
  24. >void main()
  25.  ^^^^
  26. main() is the only function you *have* to return int.
  27.  
  28. >{
  29. >    FILE *File1;
  30. >    FILE *File2;
  31. >
  32. >    File1 =   fopen("FILENAME", "r+");
  33. >    
  34. >    /* loop through lines in File1 using fgets() */
  35. >
  36. >    if (Condition) /* just indicating some condition was met */
  37. >    {
  38. >        File2 = fopen("FILENAME2", "r+");
  39. >      
  40. >        fgets(Line, File2); <----- As soon as this occurs, File1 gets
  41. >                                          wiped out.  Why?
  42.  
  43. I have to believe you're not including <stdio.h> or your compiler is not ANSI 
  44. C compliant, otherwise you would certainly get an error. The declaration for 
  45. fgets() is:
  46.  
  47. char *fgets(char *string,int num,FILE *fileptr);
  48.  
  49. Assuming Line is the string, you have left out the num value that limits the 
  50. number of bytes (chars) read. fgets() will read characters up to and including 
  51. a '\n' character or num-1 (whichever comes first) and append a '\0'. Make sure 
  52. you include necessary header files so you don't have headeraches. :)
  53.  
  54. You should also make sure that the FILE *'s are valid by comparing them to 
  55. NULL before using them.
  56.  
  57.  
  58. >    }
  59.  
  60. Throw in a:
  61.     return 0;
  62. here.
  63.  
  64. >}
  65. >
  66. >Please send any and all replies through email to
  67. >                roberino@aol.com.
  68. >I appreciate any and all help.
  69. >
  70. >Thank you,
  71. >Rob
  72.  
  73.  
  74. Bill
  75.  
  76. "Whatcha got on?...Your mind?"
  77.